package com.ruoyi.project.module.zxm.tool;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.library.api.RemoteLibraryService;
import com.ruoyi.library.api.domain.YhMsgInner;
import com.ruoyi.project.domain.YqgInformationRoom;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
/*
       定时任务的工具类，这块使用的用时间做定时任务
       判断当前时间和传进来时间的差值，然后在多少时间后执行
 */
@Component
public class SchedulerTool {

    @Resource
    private RemoteLibraryService remoteLibraryService;

    private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public void wakeupAt(YqgInformationRoom yqgInformationRoom, String timeStr) {
        try {
            Date wakeupTime = sdf.parse(timeStr);

            Runnable task = () -> {
                // 这块使用websocket发送消息
                extracted(yqgInformationRoom);

                // 在这里执行到时提醒的逻辑，例如语音合成
                System.out.println("Time's up!");
            };

            long delayInSeconds = (wakeupTime.getTime() - System.currentTimeMillis()) / 1000;

            // 提交任务给调度器
            scheduler.schedule(task, delayInSeconds, TimeUnit.SECONDS);

        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }

    public void latter(YqgInformationRoom yqgInformationRoom, Date wakeupTime) {
        try {


            Runnable task = () -> {
                // 这块使用websocket发送消息
                extracted1(yqgInformationRoom);

                // 在这里执行到时提醒的逻辑，例如语音合成
                System.out.println("Time's up!");
            };

            long delayInSeconds = (wakeupTime.getTime() - System.currentTimeMillis()) / 1000;

            // 提交任务给调度器
            scheduler.schedule(task, delayInSeconds, TimeUnit.SECONDS);

        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
    }

    private void extracted(YqgInformationRoom yqgInformationRoom) {
        YhMsgInner msg=new YhMsgInner();
        msg.setMsgTitle("0");
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getSysUser();
        msg.setMsgContent(yqgInformationRoom.getRoomNumber()+"叫醒到时");
        msg.setSendUserName("admin");
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add("admin");
        receiveNames.add("ry");
        remoteLibraryService.PushMag(msg,receiveNames);
    }

    private void extracted1(YqgInformationRoom yqgInformationRoom) {
        YhMsgInner msg=new YhMsgInner();
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getSysUser();
        msg.setMsgContent(yqgInformationRoom.getRoomNumber()+"休息到时");
        msg.setSendUserName("admin");
        msg.setMsgTitle("1");
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add("admin");
        receiveNames.add("ry");
        remoteLibraryService.PushMag(msg,receiveNames);
    }

    public void shutdown() {
        scheduler.shutdown();
    }

}
